package com.sbr.esbmonitor.wicket.pages.queues.messages;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.esbmonitor.activemq.model.Queue;
import com.sbr.esbmonitor.activemq.repository.jmx.impl.JMXQueueRepository;
import com.sbr.esbmonitor.repositories.xml.impl.XMLServerRepository;
import com.sbr.esbmonitor.servicemix.Server;
import com.sbr.esbmonitor.wicket.pages.BasePage;

public class ListMessagesPage extends BasePage {

	private static final Logger LOG = LoggerFactory.getLogger(ListMessagesPage.class);

	public ListMessagesPage(final PageParameters params) {
		
		
		final Long serverId = params.getLong("server");
		LOG.debug("serverId = {}",params.get("server"));
		
		final Server server = new XMLServerRepository().selectById(serverId);
		
		
		IModel<List<Queue>> model = new LoadableDetachableModel<List<Queue>>() {

			private static final long serialVersionUID = 1L;

			@Override
			protected List<Queue> load() {
				return new JMXQueueRepository().selectAll(server);
			}
		};
		
		ListView<Queue> queues = new ListView<Queue>("queue", model) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Queue> item) {

				
				//TODO CompoundMOdel ??? comme dans EditPAge
				Queue queue = item.getModelObject();
				
				item.add(new Label("name", queue.getName()));
				item.add(new Label("consumerCount",""+queue.getConsumerCount()));
				item.add(new Label("size", ""+queue.getQueueSize()));
			}
		};
		
		add(queues);
		
	}
}
